package name.fowler.refactoring.simplestore;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.FlushModeType;
import javax.persistence.Persistence;

public class MovieDatabaseService {
	private EntityManager em;

	public MovieDatabaseService() {
		this.em = Persistence.createEntityManagerFactory("MovieRent").createEntityManager();
	}	
	
	public List<Customer> selectAllCustomers() {
		return em.createQuery("SELECT c FROM Customer c").getResultList();
	}
	
	public void insertAllCustomers(List<Customer> customers) {
		em.setFlushMode(FlushModeType.COMMIT);
		em.getTransaction().begin();
		for (Customer customer : customers){
			em.persist(customer);
		}
		em.getTransaction().commit();
	}
}
